import tinymce from 'tinymce'

import 'tinymce/themes/silver'
import 'tinymce/icons/default/icons'
import 'tinymce/plugins/advlist'
import 'tinymce/plugins/anchor'
import 'tinymce/plugins/autolink'
import 'tinymce/plugins/autosave'
import 'tinymce/plugins/code'
import 'tinymce/plugins/codesample'
import 'tinymce/plugins/directionality'
import 'tinymce/plugins/fullscreen'
import 'tinymce/plugins/hr'
import 'tinymce/plugins/insertdatetime'
import 'tinymce/plugins/link'
import 'tinymce/plugins/lists'
import 'tinymce/plugins/media'
import 'tinymce/plugins/nonbreaking'
import 'tinymce/plugins/noneditable'
import 'tinymce/plugins/pagebreak'
import 'tinymce/plugins/paste'
import 'tinymce/plugins/preview'
import 'tinymce/plugins/print'
import 'tinymce/plugins/save'
import 'tinymce/plugins/searchreplace'
import 'tinymce/plugins/tabfocus'
import 'tinymce/plugins/table'
import 'tinymce/plugins/image'
import 'tinymce/plugins/template'
import 'tinymce/plugins/textpattern'
import 'tinymce/plugins/visualblocks'
import 'tinymce/plugins/visualchars'
import 'tinymce/plugins/wordcount'

/** 作为独立使用时快速生成默认配置 */
export function getTinymceBaseConfig(): Record<string, any> {
  const publicPath = import.meta.env.VITE_PUBLIC_PATH
  console.log('publicPath', publicPath)
  const resource =
    import.meta.env.MODE === 'prod'
      ? `${import.meta.env.VITE_BASE_URL_RICH}/system/tinymce`
      : `${publicPath}tinymce`

  // const lang = 'zh_CN'
  const theme = 'oxide'
  const fontFormats = [
    // ['方正小标宋_GBK', '方正小标宋_GBK'],
    // ['方正仿宋_GBK', '方正仿宋_GBK'],
    // ['方正黑体_GBK', '方正黑体_GBK'],
    // ['方正楷体_GBK', '方正楷体_GBK'],
    ['微软雅黑', '微软雅黑'],
    ['楷体', '楷体'],
    ['宋体', '宋体'],
    ['新宋体', 'NSimSun'],
    ['仿宋', '仿宋'],
    ['黑体', '黑体']
    // ['Corbel', 'Corbel'],
    // ['DejaVu Sans Mono', 'DejaVu Sans Mono']
    // ['思源黑体 CN', '思源黑体 CN'],
    // ['仿宋_GB2312', '仿宋_GB2312'],
    // ['楷体_GB2312', '楷体_GB2312'],
    // ['方正小标宋简体', '方正小标宋简体'],
    // ['Time New Roman', 'Time New Roman']
  ]

  return {
    placeholder: '测试工会',
    skin_url: `${resource}/skins/ui/${theme}`,
    branding: false,
    convert_urls: false,
    font_formats: fontFormats.map((f) => f.join('=')).join(';'),
    codesample_preview: true,
    default_link_target: '_blank',
    link_title: false,
    object_resizing: false,
    auto_focus: true,
    language: 'zh_CN',
    content_css: `${resource}/skins/content/default/content.css`,
    language_url: resource + '/langs/zh_CN.js',
    lineheight_formats: '10pt 12pt 14pt 16pt 18pt 24pt 31pt 36pt', // 使用pt为单位设置行间距选项
    fontsize_formats: wordFontSizeSelect
      .map((item) => `${item.title}=${item.pt}pt`)
      .join(' '),
    plugins: 'image,paste',
    paste_auto_cleanup_on_paste: true,
    paste_remove_styles: true,
    paste_remove_styles_if_webkit: true,
    paste_strip_class_attributes: true,
    toolbar:
      'bold italic underline strikethrough | fontsizeselect fontselect lineheight | forecolor backcolor image | alignleft aligncenter alignright alignjustify | bullist numlist | outdent indent blockquote | undo redo | link unlink  code | removeformat'
  }
}

const wordFontSizeSelect = [
  { title: '初号', pt: 42 },
  { title: '小初', pt: 36 },
  { title: '一号', pt: 26 },
  { title: '小一', pt: 24 },
  { title: '二号', pt: 22 },
  { title: '小二', pt: 18 },
  { title: '三号', pt: 16 },
  { title: '小三', pt: 15 },
  { title: '四号', pt: 14 },
  { title: '小四', pt: 12 },
  { title: '五号', pt: 10.5 },
  { title: '小五', pt: 9 },
  { title: '六号', pt: 7.5 },
  { title: '小六', pt: 6.5 },
  { title: '七号', pt: 5.5 },
  { title: '八号', pt: 5 }
]

export function initInlineTinymce(options?: Record<string, any>) {
  return tinymce.init({
    inline: true,
    menubar: false,
    ...getTinymceBaseConfig(),
    ...options
  })
}
